using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine;



public class 一键修改stander_Lit : EditorWindow
{
   [MenuItem("万俟工具箱/一键替换成URP材质球", false)]
   public static void Ini()
   {
      var win = EditorWindow.GetWindow<一键修改stander_Lit>();
      win.Show();
      
      var find = AssetDatabase.FindAssets("t:Material");
      var findPath =find.Select(s=>AssetDatabase.GUIDToAssetPath(s));
      
      foreach (var VARIABLE in findPath)
      {
         var m = AssetDatabase.LoadAssetAtPath<Material>(VARIABLE);
         if (!win.寻找到的shaders.ContainsKey(m.shader.name))
         {
            win.寻找到的shaders.Add(m.shader.name,Shader.Find(m.shader.name));
         }
      }

      win.shadersKeys = win.寻找到的shaders.Keys.ToArray();
   }

   private string[] shadersKeys;
   private Dictionary<string,Shader> 寻找到的shaders = new Dictionary<string, Shader>();
   private int a;
   private int b;
   private void OnGUI()
   {
      // EditorGUI.BeginChangeCheck();
      a = EditorGUILayout.Popup(a, shadersKeys);
      if (GUILayout.Button("转换"))
      {
         全部转换(寻找到的shaders[shadersKeys[a]], 寻找到的shaders[shadersKeys[b]]);
      }
      
      b = EditorGUILayout.Popup(b, shadersKeys);
   }

   private void 全部转换(Shader a, Shader b)
   {
      var find = AssetDatabase.FindAssets("t:Material");
      var findPath =find.Select(s=>AssetDatabase.GUIDToAssetPath(s));
      
      foreach (var VARIABLE in findPath)
      {
         var m = AssetDatabase.LoadAssetAtPath<Material>(VARIABLE);
         if (m.shader == a)
         {
            bool 有颜色=false;
            Color mc = Color.white;
            if (m.HasProperty("_Color"))
            {
               Debug.Log("有颜色");
               
               mc = m.color;
            }
            
            
            Texture mt = m.mainTexture;

            m.shader = b;
            m.color = mc;
            m.mainTexture = mt;
            Debug.Log(m);
         }
      }
   }

   [MenuItem("万俟工具箱/清楚缓存", false)]
   public static void 清楚缓存()
   {
      PlayerPrefs.DeleteAll();
      PlayerPrefs.Save();
   }
}
